myApp.controller("FamilyCtrl", ['$scope', '$http', '$routeParams', '$restUrls', '$post', '$rootScope', function ($scope, $http, $routeParams, $restUrls, $post, $rootScope) {
    $scope.Userinfo = {
        lastname: '',
        telephone: '',
        addressformat: '',
        birthday: '',
        choose_country: {},
        choose_zone: {},
        choose_district:{}
    };
    $scope.CountryList = [];
    $scope.ZoneList = [];
    $scope.DistrictList = [];
    //获取用户信息
    $scope.GetCustomerInfo = function () {
        $http({ method: "GET", url: $restUrls["getCustomerInfo"], params: {} }).success(function (result) {
            if (result.success == 1) {
                var UserProfile = result.data;
                $scope.Userinfo.lastname = UserProfile.lastname;
                $scope.Userinfo.telephone = UserProfile.telephone;
                $http({ method: "GET", url: $restUrls["getCountry"], params: {} }).success(function (result) {
                    if (result.success == 1) {
                        $scope.CountryList = result.data;
                        for (var i = 0; i < $scope.CountryList.length; i++) {
                            if ($scope.CountryList[i].country_id == UserProfile.country_id) {
                                $scope.Userinfo.choose_country = $scope.CountryList[i];
                                $scope.Userinfo.addressformat = $scope.Userinfo.choose_country.name;
                            }
                        }
                        $http({ method: "GET", url: $restUrls["getZone"], params: { country_id: UserProfile.country_id } }).success(function (result) {
                            if (result.success == 1) {
                                var ZoneList = result.data;
                                for (var i = 0; i < ZoneList.length; i++) {
                                    if (ZoneList[i].zone_id == UserProfile.zone_id) {
                                        $scope.Userinfo.choose_zone = ZoneList[i];
                                        $scope.Userinfo.addressformat = $scope.Userinfo.addressformat + $scope.Userinfo.choose_zone.name;
                                        $http({ method: "GET", url: $restUrls["getDistrict"], params: { zone_id: UserProfile.zone_id } }).success(function (result) {
                                            if (result.success == 1) {
                                                var DistrictList = result.data;
                                                for (var i = 0; i < DistrictList.length; i++) {
                                                    if (DistrictList[i].district_id == UserProfile.district_id) {
                                                        $scope.Userinfo.choose_district = DistrictList[i];
                                                        $scope.Userinfo.addressformat = $scope.Userinfo.addressformat + $scope.Userinfo.choose_district.name;
                                                    }
                                                }
                                            }
                                        })
                                    }
                                }
                            }
                        })
                    }
                });
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    $scope.GetCustomerInfo();
    //获取省级数据
    $scope.GetCountryList = function () {
        $http({ method: "GET", url: $restUrls["getCountry"], params: {} }).success(function (result) {
            if (result.success == 1) {
                $scope.CountryList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择省
    $scope.ChooseCountry = function (country) {
        $scope.ProvinceShow = false;
        $scope.CityShow = true;
        $scope.Userinfo.choose_country = country;
        $http({ method: "GET", url: $restUrls["getZone"], params: { country_id: country.country_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.ZoneList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择市
    $scope.ChooseZone = function (zone) {
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = true;
        $scope.Userinfo.choose_zone = zone;
        $http({ method: "GET", url: $restUrls["getDistrict"], params: { zone_id: zone.zone_id } }).success(function (result) {
            if (result.success == 1) {
                $scope.DistrictList = result.data;
            }
        }).error(function (data, status, headers, config) {
            $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
        });
    }
    //选择区
    $scope.ChooseDistrict = function (district) {
        $scope.ProvinceShow = false;
        $scope.CityShow = false;
        $scope.CountryShow = false;
        $scope.AddressShow = false;
        $scope.Userinfo.choose_district = district;
        $scope.Userinfo.addressformat = $scope.Userinfo.choose_country.name + $scope.Userinfo.choose_zone.name + $scope.Userinfo.choose_district.name;

    }
    //保存家长信息
    $scope.SaveParentInfo = function () {
        if ($scope.UserInfoForm.$valid) {
            var parameters = {
                telephone:$scope.Userinfo.telephone,
                lastname: $scope.Userinfo.lastname,
                country_id:$scope.Userinfo.choose_country.country_id,
                zone_id: $scope.Userinfo.choose_zone.zone_id,
                district_id: $scope.Userinfo.choose_district.district_id
            }
            $post($restUrls["editParent"], parameters).success(function (result) {
                if (result.success == 1) {
                    window.history.back();
                } else {
                    $.scojs_message(result.message, $.scojs_message.TYPE_ERROR);
                }
            }).error(function (data, status, headers, config) {
                $.scojs_message('服务器忙,请稍候重试', $.scojs_message.TYPE_ERROR);
            });
        }
        else {
            $scope.showOneError = true;
        }
    }



}]);/**
 * Created by zhengrunqiang on 2016-12-19.
 */
